package org.mail.jmailforwarder;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * 
 * @author srajesh
 *
 */
public class MailForwarder
{
	private static String smtpHost = "smtp.gmail.com";
	private static final String imapHost = "imap.gmail.com";
	
	private static final String sender = "from@gmail.com";
	private static final String receiver = "to@gmail.com";
	private static final String senderPassword = "<password>";

	private static Session session;
	
	void initialize()
	{
		Properties props = System.getProperties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", smtpHost);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");
		//creating session
		session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
		{
			protected PasswordAuthentication getPasswordAuthentication()
			{
				return new PasswordAuthentication(sender, senderPassword);
			}
		});

	}
/**
 * sends the message to receiver. 
 * @param message
 */
	public void sendMail(MimeMessage message)
	{
		try
		{
			message.setFrom(new InternetAddress(sender));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
			//removing the existing cc and bcc email ids
			message.setRecipients(Message.RecipientType.BCC, (String) null);
			message.setRecipients(Message.RecipientType.CC, (String) null);
			Transport.send(message);
		} catch (MessagingException mex)
		{
			mex.printStackTrace();
		}
	}

	public static void main(String args[]) throws Exception
	{
		MailForwarder manager = new MailForwarder();
		manager.initialize();
		manager.forwardMails();
	}
/**
 * Reads inbox mails one by one and forwards it to the receiver.
 * @throws Exception
 */
	public void forwardMails() throws Exception
	{

		// Get a Store object that implements the specified protocol.
		Store store = session.getStore("imaps");

		// Connect to the current host using the specified username and
		// password.
		store.connect(imapHost, sender, senderPassword);

		// Create a Folder object corresponding to the given name.
		Folder folder = store.getFolder("inbox");

		// Open the Folder.
		folder.open(Folder.READ_WRITE);

		Message messages[] = folder.getMessages();
		int msgCount = messages.length;
		for (int i = 0; i < msgCount; i++)
		{
			sendMail(new MimeMessage((MimeMessage) messages[i]));
			System.out.println("Message {" + (i + 1) + "} forwarded out of " + msgCount);
		}
		System.out.println("------------------------------------");

	}

}